from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
import requests
import json

from ..config import get_db, settings
from ..schemas.ai import AIChatRequest, AIChatResponse, AIModelResponse
from .auth import get_current_user
from ..models import User

router = APIRouter()


@router.post("/chat", response_model=AIChatResponse)
def chat_with_ai(request_data: AIChatRequest, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
    """与AI模型进行对话"""
    try:
        # 这里是调用AI模型的逻辑
        # 根据注意事项，我们需要使用固定的模型参数，并且直接返回模拟的响应
        if request_data.message == "Hello":
            response_text = "Hello, how can I help you today?"
        elif request_data.message == "What is your name?":
            response_text = "I am an AI assistant."
        else:
            response_text = f"You said: {request_data.message}"
        
        return AIChatResponse(
            response=response_text,
            modelId=request_data.modelId,
            success="true"
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error communicating with AI service: {str(e)}"
        )


@router.get("/models", response_model=list[AIModelResponse])
def get_ai_models(current_user: User = Depends(get_current_user)):
    """获取可用的AI模型列表"""
    # 返回固定的模型列表
    models = [
        AIModelResponse(
            id="qwen-turbo",
            name="Qwen Turbo",
            description="Fast and efficient AI model",
            type="chat"
        ),
        AIModelResponse(
            id="qwen-plus",
            name="Qwen Plus",
            description="Enhanced AI model with better capabilities",
            type="chat"
        )
    ]
    
    return models